<template>
	<div class="system-dic-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
			<el-form ref="dicDialogFormRef" :model="state.ruleForm" size="default" label-width="90px" :rules="rules">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="字典名称" prop="dicName">
							<el-input v-model="state.ruleForm.dicName" placeholder="请输入字典名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="字段名" prop="dicType">
							<el-input v-model="state.ruleForm.dicType" placeholder="请输入字段名，拼接 ruleForm.list"
								clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="字典状态" prop="status">
							<el-switch v-model="state.ruleForm.status" inline-prompt active-text="启"
								inactive-text="禁"></el-switch>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-row :gutter="15" v-for="(v, k) in state.ruleForm.list" :key="k">
							<el-col :xs="24" :sm="10" :md="10" :lg="10" :xl="10" class="mb20">
								<el-form-item :prop="'list.' + k + '.label'" :rules="rules.dictItemLabel">

									<template #label>
										<span>字段</span>
									</template>

									<el-input v-model="v.label" style="width: 100%" placeholder="请输入字段名"></el-input>

								</el-form-item>
							</el-col>

							<el-col :xs="24" :sm="6" :md="6" :lg="6" :xl="6" class="mb20">
								<el-form-item label="属性" :prop="`list[${k}].code`" label-width="50px"
									:rules="rules.dictItemCode">
									<el-input-number :min="1" v-model="v.code" style="width: 100%" placeholder="请输入属性值"> </el-input-number>
								</el-form-item>
							</el-col>
							<el-col :xs="24" :sm="8" :md="8" :lg="8" :xl="8" class="mb20">
								<el-form-item label-width="40px">
									<el-button type="primary" circle size="small" @click="onAddRow">
										<el-icon>
											<ele-Plus />
										</el-icon>
									</el-button>
									<el-button type="danger" circle size="small" @click="onDelRow(k)">
										<el-icon>
											<ele-Delete />
										</el-icon>
									</el-button>
									<el-button type="primary" circle size="small" @click="onMove(k, 1)" :disabled="k === 0">
										<el-icon>
											<ele-ArrowUpBold />
										</el-icon>
									</el-button>
									<el-button type="primary" circle size="small" @click="onMove(k, 0)"
										:disabled="k === state.ruleForm.list.length - 1">
										<el-icon>
											<ele-ArrowDownBold />
										</el-icon>
									</el-button>
								</el-form-item>
							</el-col>
						</el-row>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="字典描述" prop="brief">
							<el-input v-model="state.ruleForm.brief" type="textarea" placeholder="请输入字典描述"
								maxlength="150"></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup name="systemDicDialog">
import { reactive, ref, nextTick } from 'vue';
import { useSystemApi } from '/@/api/system';
import { ElMessage } from 'element-plus';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);
const systemApi = useSystemApi();

// 定义变量内容
const dicDialogFormRef = ref();
const state = reactive({
	ruleForm: {
		id: '',
		dicName: '', // 字典名称
		dicType: '', // 字段名
		status: true, // 字典状态
		list: [], // 子集字段 + 属性值
		brief: '', // 字典描述
	},
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});


const rules = reactive({
	dicName: [
		{ required: true, message: '字典名称不能为空', trigger: 'blur' },
	],
	dicType: [
		{ required: true, message: '字典类型不能为空', trigger: 'blur' },
	],

	dictItemLabel: [
		{ required: true, message: "请输入字段", trigger: "blur" },
	],

	dictItemCode: [
		{ required: true, message: "请输入属性", trigger: "blur" },
	],
});

// 打开弹窗
const openDialog = (type, id) => {
	state.dialog.type = type;
	if (type === 'edit') {
		state.dialog.title = '修改字典';
		state.dialog.submitTxt = '修 改';

		systemApi.dictDataInfo(id).then(res => {
			state.ruleForm.dicType = res.data.dicType;
			state.ruleForm.dicName = res.data.dicName;
			state.ruleForm.id = res.data.id;
			state.ruleForm.list = res.data.list;
			state.ruleForm.status = res.data.status;
			state.ruleForm.brief = res.data.brief;
			if (!state.ruleForm.list || state.ruleForm.list.length <= 0) {
				onAddRow()
			}
		})

	} else {
		state.dialog.title = '新增字典';
		state.dialog.submitTxt = '新 增';
		// 清空表单，此项需加表单验证才能使用
		nextTick(() => {
			dicDialogFormRef.value.resetFields();
			state.ruleForm.list = [];
			state.ruleForm.list.push({
				id: 0,
				label: '',
				code: '',
			});
		});
	}
	state.dialog.isShowDialog = true;
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = () => {

	if (!dicDialogFormRef.value) return

	dicDialogFormRef.value.validate((valid) => {

		if (valid) {


			if (state.dialog.type === 'edit') {
				systemApi.dictDataUpdate({
					'id': state.ruleForm.id,
					'dicName': state.ruleForm.dicName,
					'dicType': state.ruleForm.dicType,
					'status': state.ruleForm.status,
					'list': state.ruleForm.list,
					'brief': state.ruleForm.brief,
				}).then(res => {
					closeDialog();
					emit('refresh');
				})
			} else {
				systemApi.dictDataAdd({
					'dicName': state.ruleForm.dicName,
					'dicType': state.ruleForm.dicType,
					'status': state.ruleForm.status,
					'list': state.ruleForm.list,
					'brief': state.ruleForm.brief,
				}).then(res => {
					closeDialog();
					emit('refresh');
				})
			}
		}
	})



};

// 上移，下移
const onMove = (crtInx, dir) => {
	let moveComm = (curIndex, nextIndex) => {
		let arr = state.ruleForm.list
		arr[curIndex] = arr.splice(nextIndex, 1, arr[curIndex])[0]
		return arr
	}
	state.ruleForm.list.some((val, index) => {
		if (index === crtInx) {
			if (dir === 1 && index === 0) {
				ElMessage.warning("已在顶部")
			} else if (dir === 0 && index === state.ruleForm.list.length - 1) {
				ElMessage.warning("已在底部")
			} else {
				let nextIndex = dir === 1 ? index - 1 : index + 1
				state.ruleForm.list = moveComm(index, nextIndex)
			}
			return true
		}
		return false
	})
}

// 新增行
const onAddRow = () => {
	state.ruleForm.list.push({
		id: 0,
		label: '',
		code: '',
	});
};
// 删除行
const onDelRow = (k) => {
	state.ruleForm.list.splice(k, 1);
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
